//
//  ZPSelectGradeVC.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/2/6.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPSelectGradeVC.h"
#import "ZPRegSelectCityAndGradeCell.h"
#import "ZPRegSelectGrade.h"

@interface ZPSelectGradeVC ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITableView *gradeTableView;
@property (copy, nonatomic) NSMutableArray *grades;
@end

@implementation ZPSelectGradeVC


- (NSMutableArray *)grades
{
    if (_grades == nil) {
        _grades = [NSMutableArray array];
    }
    return _grades;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getGradesList];
    self.gradeTableView.delegate = self;
    self.gradeTableView.dataSource = self;
    ViewRadius(_contentView, 4);
}

#pragma mark - <获取年级和城市>
- (void)getGradesList
{
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"user/getSelect.json" parameters:@{} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        if ([[responseObject objectForKey:@"code"] integerValue] ==200) {
            if ([responseObject objectForKey:@"result"]) {
                // 返回城市
                NSArray *resultGrades  = [ZPRegSelectGrade mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"grade"]];
                if (resultGrades.count) {
                    [self.grades addObjectsFromArray:resultGrades];
                }
            }
        }
        // 重载
        [self.gradeTableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        
        [ZPPromptView showMessage:@"请检查您的网络设置"];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.grades.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const reuseIdentifier  = @"ZPRegSelectCityAndGradeCell";
    ZPRegSelectCityAndGradeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPRegSelectCityAndGradeCell class]) owner:self options:nil] lastObject];
    }
    ZPRegSelectGrade *grade = self.grades[indexPath.row];
    cell.selectLab.text = grade.name;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.valueBlock1([self.grades[indexPath.row] name],[self.grades[indexPath.row] selectId]);
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)closeBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
